//通过node.js发送请求给 图灵api 获取聊天回复内容
//1. 引入http模块
var http = require("http");

var data = JSON.stringify({
    "perception": {
        "inputText": {
            "text": "你好啊"
        }
    },
    "userInfo": {
        "apiKey": "94bcd348d35d4025a456dde81fd4826b",
        "userId": "338963"
    }
})

var req = http.request({
    method: "POST",//发送post请求
    host: "openapi.tuling123.com",//域名地址
    protocal: "http://",//协议
    port: "80",//端口
    path: "/openapi/api/v2",//路径
    headers: {  //请求头
        "Content-Type": 'application/json',   
        "Content-Length": Buffer.byteLength(data)
    }  
}, function(res){           //服务器响应的时候,自动调用(异步执行)
// res是机器人 返回给你数据
    var bufferList = [];
    res.on("data", function(chunk){
        bufferList.push(chunk);
    })

    res.on("end", function(){
        var result = Buffer.concat(bufferList);
        console.log(result.toString());
    })  

})

// 发送请求到服务端数据接口
req.write(data);